package ynu.sxp.demoapp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.sxp.demoapp.entity.Orders;
import ynu.sxp.demoapp.service.OrderService;
import ynu.sxp.demoapp.common.Result;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/OrdersController")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 创建订单
    @PostMapping("/createOrders")
    public Result<Orders> createOrders(@RequestBody Orders order) {
        Orders createdOrder = orderService.createOrder(order);
        return Result.success(createdOrder);
    }

    // 根据订单ID获取订单信息
    @GetMapping("/getOrdersById")
    public Result<Orders> getOrdersById(@RequestParam UUID orderId) {
        Orders order = orderService.getOrderById(orderId);
        return Result.success(order);
    }

    // 获取用户的所有订单
    @GetMapping("/listOrdersByUserId")
    public Result<List<Orders>> listOrdersByUserId(@RequestParam String userId) {
        List<Orders> orders = orderService.getOrdersByUserId(userId);
        return Result.success(orders);
    }

    // 更新订单信息
    @PostMapping("/updateOrder")
    public Result<Void> updateOrder(@RequestBody Orders order) {
        orderService.updateOrder(order);
        return Result.success(null);
    }

    // 删除订单
    @DeleteMapping("/deleteOrder")
    public Result<Void> deleteOrder(@RequestParam UUID orderId) {
        orderService.deleteOrder(orderId);
        return Result.success(null);
    }
}